import tkinter as tk
import rtoml
import os
class VarBind:
    def __init__(self,master) -> None:
        self.vardict_bool={}
        self.vardict_int={}
        self.vardict_str={}
        for idx in ["rts_bool","dtr_bool","hexshow_bool","datatofile_bool"
        ,"topmost_bool","timestap_bool","hexsend_bool","stabletimesend_bool","enterreturn_bool","stri_bool"]:
            self.vardict_bool[idx]=tk.BooleanVar(master)
        for idx in ["bound_int","overlaytime_int","stabletime_int"]:
            self.vardict_int[idx]=tk.IntVar(master)
        self.vardict_str["stri_str"]=tk.StringVar(master)
        self.load()
    def load(self):
        if os.path.exists("config.toml"):
            common=rtoml.load(open("config.toml","r").read())
            print(common)
            for idx in ["rts_bool","dtr_bool","hexshow_bool","datatofile_bool"
            ,"topmost_bool","timestap_bool","hexsend_bool","stabletimesend_bool","enterreturn_bool"]:
                self.vardict_bool[idx].set(common["bool"][idx])
            for idx in ["bound_int","overlaytime_int","stabletime_int"]:
                self.vardict_int[idx].set(common["int"][idx])
            self.vardict_str["stri_str"].set(common["str"]["stri_str"])
            
        # common = {"int":iinntt,"bool":bbooll}
        else:
            self.save()
        
    def save(self):
        bbooll ={}
        for idx in ["rts_bool","dtr_bool","hexshow_bool","datatofile_bool"
        ,"topmost_bool","timestap_bool","hexsend_bool","stabletimesend_bool","enterreturn_bool"]:
            bbooll[idx]=self.vardict_bool[idx].get()
        iinntt={}
        for idx in ["bound_int","overlaytime_int","stabletime_int"]:
            iinntt[idx]=self.vardict_int[idx].get()
        ssttrr={}
        ssttrr["stri_str"]=self.vardict_str["stri_str"].get()
        
        common = {"int":iinntt,"bool":bbooll,"str":ssttrr}
        rtoml.dump(common,open("config.toml","w"),pretty=True)

if __name__=="__main__":
    win = tk.Tk()

    xe = VarBind(win)
    xe.load()

    win.mainloop()